//
//  Nav_NavigateToHandler.swift
//  mall-ios
//
//  Created by Meak Tsui on 2018/12/5.
//  Copyright © 2018 Meak Tsui. All rights reserved.
//

import Foundation
import Shared
import MTHybrid

class Nav_ToHandler: MTEventHandler {
    var ns: Ns {
        return .Nav
    }
    
    var action: Api {
        return .To
    }
    
    func handler(_ msg: MTHybridMsg) {
        guard let url = msg.params["url"] as? String else {
            dPrint("url is nil")
            msg.callbackClosure?(["success":false])
            return
        }
        guard let requestUrl = URL(string: url,relativeTo: msg.webview.url) else {
            dPrint("url illegal")
            msg.callbackClosure?(["success":false])
            return
        }
        guard let nav = msg.vc.navigationController else {
            dPrint("navigation is nil")
            msg.callbackClosure?(["success":false])
            return
        }
        
        let hideTab = msg.params["hideTab"] as? Bool ?? true
        
        let vc = HybridWebVc()
        vc.requestURL = requestUrl
        vc.hidesBottomBarWhenPushed = hideTab

//        nav.pushViewController(vc, animated: true)
        nav.redirectTo(vc, animated: true)
        
        msg.callbackClosure?(["success":true])
    }
    
}
